package com.xhlj.controller;

import com.aliyuncs.exceptions.ClientException;

import com.xhlj.annotation.SystemLog;
import com.xhlj.entity.FileOss;
import com.xhlj.service.FileService;
import com.xhlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yzc
 * @description TODO:实现文件的上传、下载、文件列表获取等功能
 * @create 2024/3/19
 **/


@RestController
@RequestMapping("file")
public class FileController {
    @Value("${aliyun.endPoint}")
    private String endPoint;

    @Value("${aliyun.bucketName}")
    private String bucketName;

    @Autowired
    private FileService fileService;

    //    @ApiOperation("文件上传")
    @SystemLog(title = "文件模块", content = "文件上传")
    @PostMapping("FileUpload")
    public Result fileUpload(MultipartFile file, @RequestParam(defaultValue = "无") String fileDes, @RequestParam(defaultValue = "") String folder) throws ClientException {
        Result res = fileService.uploadFile(file, fileDes, folder);
        //返回保存路径
        return res;
    }

    //    @ApiOperation("文件下载")
    //TODO:根据文件id将文件下载到本地路径中
    @GetMapping("FileDownload")
    public Result fileDownload(@RequestParam Integer fileId, @RequestParam(defaultValue = "") String localUrl) throws ClientException {
        //返回保存路径
        return fileService.downloadFile(fileId, localUrl);
    }

    //TODO：根据文件id获取文件的相对存储路径和绝对下载路径，打开新的标签页并输入此url即可将文件下载到本地
    @GetMapping("FileUrl")
    public Result getFileUrl(@RequestParam String fileId) {
        FileOss file = fileService.getById(fileId);
        Map<String, String> map = new HashMap<>();
        map.put("url", file.getFileUrl());
        map.put("DownloadUrl", file.getFileDownloadUrl());
        return Result.ok(map);
    }

    //TODO:列举file表数据
    @GetMapping("FileList")
    private Result getFileListFromOss() {
        List<FileOss> list = fileService.list();
        return Result.ok(list);
    }

    // TODO:根据文件id删除文件，删除成功后要处理数据表
    @SystemLog(title = "文件模块",content = "文件删除")

    @DeleteMapping()
    private Result deleteFile(@RequestParam String fileId) {
        return fileService.deleteFile(fileId);
    }
}
